package exercise6;

import java.util.Scanner;

public class Exercise13_17 {
    public static void main(String []args) throws CloneNotSupportedException {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the first complex number: ");
        double a = input.nextDouble();
        double b = input.nextDouble();
        Complex c1 = new Complex(a, b);

        System.out.print("Enter the second complex number: ");
        double c = input.nextDouble();
        double d = input.nextDouble();
        Complex c2 = new Complex(c, d);

        System.out.println("(" + c1 + ")" + " + " + "(" + c2 + ")" + " = " + c1.add(c2));
        System.out.println("(" + c1 + ")" + " - " + "(" + c2 + ")" + " = " + c1.subtract(c2));
        System.out.println("(" + c1 + ")" + " * " + "(" + c2 + ")" + " = " + c1.multiply(c2));
        System.out.println("(" + c1 + ")" + " / " + "(" + c2 + ")" + " = " + c1.divide(c2));
        System.out.println("|" + c1 + "| = " + c1.abs());



    }
}
class Complex implements Cloneable{

    //数据域
    double a=0;
    double b=0;

    /**构造方法*/
    public Complex(){
    }
    public Complex(double a){
        this.a=a;
    }
    public Complex(double a,double b){
        this.a=a;
        this.b=b;
    }

    public double getRealPart() {
        return a;
    }

    public double getImaginaryPart() {
        return b;
    }


    /**四则运算*/
    public Complex add(Complex o){
        return new Complex(o.a+a,o.b+b);
    }

    public Complex subtract(Complex o){
        return new Complex(a-o.a,b-o.b);
    }

    public Complex multiply(Complex o){
        return new Complex(o.a*a-b*o.b,b*o.a+a*o.b);
    }

    public Complex divide(Complex o) {
        double newA = (a * o.a + b * o.b)
                / (Math.pow(o.a, 2.0) + Math.pow(o.b,
                2.0));
        double newB = (b * o.a - a * o.b)
                / (Math.pow(o.a, 2.0) + Math.pow(o.b,
                2.0));
        return new Complex(newA, newB);
    }

    public double abs(){
        return Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
    }

    public String toString(){
        if (b==0){
            return String.valueOf(a);
        }
        return a +"+"+ b +"i";
    }

    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

}